const sqlite3 = require('sqlite3').verbose()
const {open} = require('sqlite3')

const DATABASE_PATH = '../assets/db/mtalk-database.db'


async function openDb() {
    return open({
        filename: DATABASE_PATH,
        driver: sqlite3.Database
    })
}

async function ws_msg_selectMsgList() {
    const db = await openDb()
    return await db.all(`SELECT *
                         FROM system_ws_msg`)
}

async function ws_msg_insertMsg(msg) {
    const db = await openDb()
    return await db.run(`INSERT INTO system_ws_msg (msg)
                         VALUES (?)`, msg)
}

async function ws_msg_deleteMsg(id) {
    const db = await openDb()
    return await db.run(`DELETE
                         FROM system_ws_msg
                         WHERE id = ?`, id)
}

async function ws_msg_updateMsg(id, msg) {
    const db = await openDb()
    return await db.run(`UPDATE system_ws_msg
                         SET msg = ?
                         WHERE id = ?`, msg, id)
}


module.exports = {
    ws_msg_deleteMsg,
    ws_msg_insertMsg,
    ws_msg_selectMsgList,
    ws_msg_updateMsg
}